package com.examination.exam.controller;

import com.examination.domain.common.ResponseResult;
import com.examination.domain.exam.base.BaseEntity;
import com.examination.domain.exam.dto.ExaminationDto;
import com.examination.domain.exam.entity.Examination;
import com.examination.domain.exam.entity.Question;
import com.examination.domain.exam.vo.ExaminationVo;
import com.examination.domain.utils.BeanCopyUtils;
import com.examination.enums.AppHttpCodeEnum;
import com.examination.exam.service.ExaminationService;
import com.examination.exam.service.QuestionService;
import com.examination.exam.utils.MetaDataUtil;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.Updates;
import org.bson.conversions.Bson;
import org.bson.types.ObjectId;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.convert.MongoConverter;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@RestController
@RequestMapping("/exam/examination")
public class ExaminationController {

    @Autowired
    private ExaminationService examinationService;

    @Autowired
    private QuestionService questionService;

    @Autowired
    private MongoConverter mongoConverter;


    @GetMapping("/get/{id}")
    public ResponseResult getExaminationById(@PathVariable String id) {
        Examination examination = examinationService.getExaminationById(id);
        ExaminationVo examinationVo = BeanCopyUtils.copyBean(examination, ExaminationVo.class);
        examinationVo.setQuestionList(examination.getQuestionList());
        if (examination.getBaseEntity().getIsDelete() != 1) {
            return ResponseResult.okResult(examinationVo);
        } else return ResponseResult.errorResult(AppHttpCodeEnum.EXAMINATION_NULL);
    }

    @GetMapping("/getByUser/{userId}")
    public ResponseResult getExaminationByUser(@PathVariable String userId) {
        List<Examination> examinationById = examinationService.getExaminationByQuery(Filters.eq("baseEntity.createBy", userId));
        ArrayList<Examination> examinations = new ArrayList<>();
        for (Examination examination : examinationById) {
            if (!Objects.isNull(examination.getBaseEntity().getIsDelete()) && examination.getBaseEntity().getIsDelete() != 1)
                examinations.add(examination);
        }
        if (examinations.size() > 0)
            return ResponseResult.okResult(BeanCopyUtils.copyBeanList(examinations, ExaminationVo.class));
        else
            return ResponseResult.errorResult(AppHttpCodeEnum.EXAMINATION_NULL);
    }

    @PostMapping("/add")
    public ResponseResult addExamination(@RequestBody ExaminationDto examinationDto) {
        ArrayList<Question> questions = new ArrayList<>();
        for (String id : examinationDto.getQuestionList()) {
            questions.add(questionService.getQuestionById(id));
        }
        Examination examination = new Examination();
        BeanUtils.copyProperties(examinationDto, examination);
        examination.setQuestionList(questions);

        BaseEntity baseEntity = new BaseEntity();
        examination.setBaseEntity(baseEntity);

        Examination examination1 = MetaDataUtil.create(examination);
        examinationService.addExamination(examination1);
        return ResponseResult.okResult();
    }

    @PostMapping("/update")
    public ResponseResult updateExamination(@RequestBody Map updateMap) {
        List<Bson> updates = MetaDataUtil.update();
        List<Question> questions = new ArrayList<>();
        for (Object o : updateMap.keySet())
            if (!String.valueOf(o).equals("id"))
                if (!String.valueOf(o).equals("questionList")) {
                    updates.add(Updates.set(String.valueOf(o), updateMap.get(o)));
                } else {
                    List<String> list = (List<String>) updateMap.get("questionList");
                    for (String s : list) {
                        questions.add(questionService.getQuestionById(s));
                    }
                }
        Bson bson = Updates.combine(updates);
        long l = examinationService.updateExamination(Filters.eq("_id", new ObjectId(String.valueOf(updateMap.get("id")))), bson);
        if (questions.size() > 0) {
            Examination examinationById = examinationService.getExaminationById(updateMap.get("id"));
            examinationById.setQuestionList(questions);
            l = examinationService.replaceExamination(Filters.eq("_id", new ObjectId(String.valueOf(updateMap.get("id")))), examinationById);
        }
        return ResponseResult.okResult(l);
    }

    @DeleteMapping("/delete/{id}")
    public ResponseResult delectExamination(@PathVariable String id) {
        List<Bson> delete = MetaDataUtil.delete();
        Bson bson = Updates.combine(delete);
        long l = examinationService.updateExamination(Filters.eq("_id", new ObjectId(id)), bson);
        return ResponseResult.okResult(l);
    }

}
